package com.tanhua.manage.listener;
import java.util.Date;

import com.alibaba.fastjson.JSON;
import com.tanhua.manage.domain.Log;
import com.tanhua.manage.service.LogService;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 消息监听器，监听tanhuan-log主题中的消息
 */
@Component
@RocketMQMessageListener(topic = "tanhuan-log",consumerGroup = "tanhuan-log-consumer")
public class LogMessageListener implements RocketMQListener<String> {

    @Autowired
    private LogService logService;

    // message：消息的内容，{"date":"2021-03-11","type":"0101","userId":"20"}
    @Override
    public void onMessage(String message) {
        //1. 转换
        Map<String,String> map = JSON.parseObject(message, Map.class);
        String date = map.get("date");
        String type = map.get("type");
        Long userId = Long.valueOf(map.get("userId")+"");

        //2. 封装log对象
        Log log = new Log();
        log.setUserId(userId);
        log.setLogTime(date);
        log.setType(type);
        log.setCreated(new Date());
        log.setUpdated(new Date());

        //3. 记录日志
        logService.save(log);
    }
}
